//
//  FirstViewController.m
//  MWDateTab
//
//  Created by Jimmy on 1/30/13.
//  Copyright (c) 2013 Jimmy Bouker. All rights reserved.
//

#import "TimeViewController.h"
#import "AppDelegate.h"

@implementation TimeViewController
static TimeViewController * STATIC_TIME_VIEW_CONTROLLER;
@synthesize timesTableView, editButton, addButton;


+(TimeViewController*)GET {
    return STATIC_TIME_VIEW_CONTROLLER;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Times", @"Times");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
        
        STATIC_TIME_VIEW_CONTROLLER = self;
        
        times = [[NSMutableArray alloc] initWithContentsOfFile:[[AppDelegate GET] getFilePath:@"times"]];
        if(times==nil) {
            times = [[NSMutableArray alloc] init];
        }
    }
    return self;
}
							
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)addButtonHit:(id)sender {
    NSDate *time = [NSDate date];
    [times addObject:time];
    
    [timesTableView reloadData];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell identifier";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    
    if(indexPath.row < times.count) {
    
        NSDate * date = [times objectAtIndex:indexPath.row];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
        NSString *stringFromDate = [formatter stringFromDate:date];
        [formatter release];
        
        cell.textLabel.text = stringFromDate;
    }
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return times.count;
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{    
    return proposedDestinationIndexPath;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    @autoreleasepool {
        NSDate *dateToMove = [[times objectAtIndex:sourceIndexPath.row] autorelease];
        
        [times removeObjectAtIndex:sourceIndexPath.row];
        [times insertObject:dateToMove atIndex:destinationIndexPath.row];
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        if(indexPath.row < times.count) {
            [times removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        }
    }  
}

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

-(IBAction)editButtonHit:(id)sender {
    bool editing = !timesTableView.editing;
    if(editing)
        editButton.title = @"Done";
    else
        editButton.title = @"Edit";
    
    [timesTableView setEditing:editing animated:YES];
}

-(void)saveTimesToDisk {
    if(times) {
        [times writeToFile:[[AppDelegate GET] getFilePath:@"times"] atomically:YES];
    }
}

@end
